package com.study.bigwork.wrongQuestion.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.study.bigwork.entity.WrongQuestion;
import com.study.bigwork.homework.service.HomeworkService;
import com.study.bigwork.wrongQuestion.service.WrongQuestionService;




@Controller
@RequestMapping("/wrongquestion")
public class WrongQuestionController {
	@Resource
	private WrongQuestionService wrongQuestionService;
	@RequestMapping(value="/addWrongQuestion",method=RequestMethod.POST)
	public void addWrongQuestion(HttpServletRequest request,HttpServletResponse response) throws IOException {
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				//设置返回数据格式和编码
		        response.setContentType("application/json;charset=utf-8");
		        
		        //定义StringBuffer变量
		      	StringBuffer stringBuffer = new StringBuffer();
		        //line保存读取请求信息的当前一行，responseMessage为响应信息，返回信息
		        String line = null, responseMessage = null;
		        
		        //输出流
		        PrintWriter out = response.getWriter();
		        
		        //读取信息时会发生IO异常
		        try{
		            //BufferedReader为缓冲读取流
		            BufferedReader bufferedReader = request.getReader();
		            while((line = bufferedReader.readLine()) != null){
		                stringBuffer.append(line);
		            }
		            System.out.println(stringBuffer);
		            
		        }catch(IOException e){
		            e.printStackTrace();
		        }
		        
		        //将json数据转为String
		        Gson gson = new Gson();
		        WrongQuestion wrongQuestion =gson.fromJson(stringBuffer.toString(), WrongQuestion.class);
		        boolean b = false;
		        System.out.println(wrongQuestion.toString());
		        //调用WrongQuestionService类中addWrongQuestion方法访问数据库，并返回查询结果
		        b = wrongQuestionService.addWrongQuestion(wrongQuestion);
		        
		        responseMessage = gson.toJson(b);
		        
		        System.out.println("addWrongQuestion  对象转为json " + responseMessage);
		        //输出流将信息返回
		        out.print(responseMessage);
	}
	@RequestMapping(value="/getWrongQuestionList/{questionType}/{id}",method=RequestMethod.GET)
	public void getWrongQuestions(@PathVariable("questionType") String questionType,@PathVariable("id") String id,
			HttpServletRequest request,HttpServletResponse response) throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				//设置返回数据格式和编码
		        response.setContentType("application/json;charset=utf-8");

				//定义StringBuffer变量
		        String responseMessage = null;
		      
		        //输出流
		        PrintWriter out = response.getWriter();
		        int user_id = 0;
		        
		        String questionTypes = "";
		        
		        String tags = "";
		        String sql = "";
		      
		        user_id = Integer.parseInt(id);
		        if ("math".equals(questionType)) {
		        	questionType = "数学";
				}else {
					questionType = "英语";
				}
		        
		        List<WrongQuestion> wrongQuestions=new ArrayList<WrongQuestion>();
		        wrongQuestions = wrongQuestionService.getWrongQuestions(questionType,id);
		        System.out.println("666"+Arrays.asList(wrongQuestions));
		        Gson gson = new Gson();
		        responseMessage = gson.toJson(wrongQuestions);
		        
		        System.out.println("对象转为json " + responseMessage);
		        //输出流将信息返回
		        out.print(responseMessage);
	}
	
	
}
